﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DrugStorehouseMIS.BLL;
using DrugStorehouseMIS.Comm;


namespace DrugStorehouseMIS.QueryAndReport
{
    public partial class frmDrugInfo : Form
    {
        private Drug drugBLL;

        public Drug DrugBLL
        {
            get { return drugBLL; }
            set { drugBLL = value; }
        }

        private Inventory inventoryBLL;

        public Inventory InventoryBLL
        {
            get { return inventoryBLL; }
            set { inventoryBLL = value; }
        }
       
        public frmDrugInfo()
        {
            InitializeComponent();

            drugBLL = new Drug();
            drugBLL.DrugTypeNum = new DrugType();
            drugBLL.MeasuringUnitNum = new MeasuringUnit();

            inventoryBLL = new Inventory();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (null != dgvDrugInfo.CurrentRow)
            {
                this.GetObject();
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 获取drug的信息
        /// </summary>
        /// <returns></returns>
        private Drug GetObject()
        {
            DrugBLL.DrugNum = Convert.ToInt32(dgvDrugInfo.CurrentRow.Cells["DrugNum"].Value);
            DrugBLL.DrugTypeNum.DrugTypeName = dgvDrugInfo.CurrentRow.Cells["DrugType"].Value.ToString();
            DrugBLL.TradeName = dgvDrugInfo.CurrentRow.Cells["TradeName"].Value.ToString();
            DrugBLL.CommonName = dgvDrugInfo.CurrentRow.Cells["CommonName"].Value.ToString();
            DrugBLL.UserDefindeName = dgvDrugInfo.CurrentRow.Cells["UserDefinedName"].Value.ToString();
            DrugBLL.Sepcifications = dgvDrugInfo.CurrentRow.Cells["Specifications"].Value.ToString();
            DrugBLL.MeasuringUnitNum.MeasuringUnitName = dgvDrugInfo.CurrentRow.Cells["MeasuringUnit"].Value.ToString();
            DrugBLL.ApprovalNum = dgvDrugInfo.CurrentRow.Cells["ApprovalNum"].Value.ToString();
            DrugBLL.TradePrice = Convert.ToSingle(dgvDrugInfo.CurrentRow.Cells["TradePrice"].Value);
            DrugBLL.RetailPrice = Convert.ToSingle(dgvDrugInfo.CurrentRow.Cells["RetailPrice"].Value);
            DrugBLL.DrugCode = dgvDrugInfo.CurrentRow.Cells["DrugCode"].Value.ToString();
            DrugBLL.MaxQuantity = Convert.ToInt32(dgvDrugInfo.CurrentRow.Cells["MaxQuantity"].Value);    
            DrugBLL.MinQuantity = Convert.ToInt32(dgvDrugInfo.CurrentRow.Cells["MinQuantity"].Value);

            inventoryBLL.StockQuantity = Convert.ToInt32(dgvDrugInfo.CurrentRow.Cells["StockQuantity"].Value); //获取该药品的库存
           
            return DrugBLL;
        }

        /// <summary>
        /// 多条件查询
        /// </summary>
        private void LoadDataGridView()
        {
            dgvDrugInfo.DataSource = DrugBLL.Filter(txtDrugType.Text.Trim(), txtDrugName.Text.Trim(), txtDrugCode.Text.Trim());

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            LoadDataGridView();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtDrugCode.Text = txtDrugName.Text = txtDrugType.Text = "";
        }

        private void frmDrugInfo_Load(object sender, EventArgs e)
        {
            LoadDataGridView();
        }

        private void txtDrugName_KeyUp(object sender, KeyEventArgs e)
        {
            if(e.KeyCode==Keys.Enter)
            LoadDataGridView();
        }

        private void txtDrugType_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            LoadDataGridView();
        }

        private void txtDrugCode_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            LoadDataGridView();
        }
    }
}